<?php

namespace App\Services;

use common\utils\SysCode;
use App\Models\HallConfig;
use App\Models\User;
use App\Models\HallPartnerConfig;
use common\enum\CommonEnum;
/**
 * author: day
 * date: 2018/12/04 19:38
 * description: 公用检测各种状态合法性
 */

class CheckingService {
    /**
     * 厅主状态性判断
     */
    public static function checkHallValid($hallId)
    {
        $checkHall = HallConfig::where(['hall_id' => $hallId, 'status' => 1])->first();
        if (empty($checkHall)) {
            error_exit(SysCode::BANK_EXCHANGE_HALL_ERROR);
        }
    }

    /**
     * 检测用户有效性
     */
    public static function checkUserValid($userId)
    {
        $user = User::where(['id' => $userId])->first();
        if (empty($user)) {
            error_exit(SysCode::USER_NOT_FOUND);
        }
        return $user;
    }

    /**
     * 检测第三方 API 有效性
     */
    public static function checkPartnerValid($partnerId, $hallId, $force = false)
    {
        $partner = HallPartnerConfig::where(['partner_id' => $partnerId, 'hall_id' => $hallId])->first();
        if (empty($partner)) {
            error_exit(SysCode::API_ERROR_CONFIG);
        } elseif (!$force && $partner['status'] != CommonEnum::ENABLED) {
            error_exit(SysCode::BANK_EXCHANGE_PARTNER_ERROR);
        }
        return $partner;
    }
}